<?php

/**
 * Framework_Presenter_Smarty_PagingInterface
 *
 * @author Application Support Group
 * @copyright University of Georgia
 * @license http://www.opensource.org/licenses/gpl-license.php
 * @package BAMF
 * @filesource
*/


/**
 * Framework_Presenter_Smarty_PagingInterface
 *
 *
 * @author Application Support Group
 * @package BAMF
 * @link http://smarty.php.net
*/
class Framework_Presenter_Smarty_PagingInterface extends Framework_Presenter_Smarty_Extension {

	public function __construct() {
		parent::__construct() ;
	}

	public static function register( &$smartyObj ) {
		$smartyExtension = new Framework_Presenter_Smarty_PagingInterface() ;
		$smartyObj->register_function( "pagingInterface", array( $smartyExtension, "pagingInterface" )) ;
	}


// build a paging interface

	public function pagingInterface($params, &$smarty)
	{
		$_output = array() ;
		$pagerLimits = array( 10, 50, 100 ) ;
		
		extract($params);
	
		if (empty($pager)) {
			$smarty->trigger_error("pagingInterface: missing 'pager' parameter");
			return;
		}

		
		if(empty($get)) {
			$get = "";
		} else {
			$get= '&amp;'.$get;
		}
		
		// default "enableAll" to true
		
		if ( !isset( $enableAll )) {
			$enableAll = true ;
		}
		
			
		// set our basic loop values and number formatting
		
		$pStart = 1 ;
		$pEnd = 10 ;
		$numFormat = $pager->pagecount > 99 ? "%03d" : "%02d" ;
		$modulePath = $smarty->get_template_vars( 'modulePath' ) ;
		$itemNumber = $smarty->get_template_vars( 'itemNumber' ) ;
		$basePath = empty( $itemNumber ) ? $modulePath : $modulePath . "/" . $itemNumber ;
	

		if(isset($basehref) && !empty($basehref)) {
			$basePath = $basehref . $basePath;
		}
		
		if ( $pager->pagecount < 10 ) {
			$pEnd = $pager->pagecount ;
		} else {
			if ( $pager->currpage > 5 ) {
				$pEnd = min( $pager->pagecount, $pager->currpage + 5 ) ;
				$pStart = min( $pager->pagecount - 9, $pager->currpage - 4 ) ;
			}
		}

		$_output[] = '<div class="pager">' ;		
		$_output[] = '<span class="pageLinks">' ;
		if ( $pager->pagecount > 1 ) {
			if ( $pager->currpage != 1 ) {
				$_output[] = '<a href="' . $basePath . '?' . PAGER_KEY_PAGE . '=1&amp;' . PAGER_KEY_LIMIT . '=' . $pager->limit . $get . '">first</a>&nbsp;&nbsp;' ;
				$_output[] = '<a href="' . $basePath . '?' . PAGER_KEY_PAGE . '=' . ($pager->currpage-1) . '&amp;' . PAGER_KEY_LIMIT . '=' . $pager->limit . $get . '">prev</a>&nbsp;&nbsp;' ;
			} else {
				$_output[] = '<span class="disabledLink">first&nbsp;&nbsp;prev&nbsp;&nbsp;</span>' ;
			}
			
			for ( $i = $pStart ; $i <= $pEnd ; $i++ ) {
				if ( $i == $pager->currpage ) {
					$_output[] = '<span class="currentPage">'.sprintf($numFormat, $i).'</span>&nbsp;&nbsp;' ;
				} else {
					$_output[] = '<a href="'.$basePath.'?'.PAGER_KEY_PAGE.'='.$i.'&amp;'.PAGER_KEY_LIMIT.'='.$pager->limit.$get.'">'.sprintf($numFormat, $i).'</a>&nbsp;&nbsp;' ;
				}
			}
			
			if ( $pager->currpage != $pager->pagecount && $pager->pagecount != 0 ) {
				$_output[] = '<a href="'.$basePath.'?'.PAGER_KEY_PAGE.'='.($pager->currpage+1).'&amp;'.PAGER_KEY_LIMIT.'='.$pager->limit.$get.'">next</a>&nbsp;&nbsp;' ;
				$_output[] = '<a href="'.$basePath.'?'.PAGER_KEY_PAGE.'='.$pager->pagecount.'&amp;'.PAGER_KEY_LIMIT.'='.$pager->limit.$get.'">last</a>&nbsp;&nbsp;' ;
			} else {
				$_output[] = '<span class="disabledLink">next&nbsp;&nbsp;last&nbsp;&nbsp;</span>' ;
			}

		}
		$_output[] = '</span>' ;
		
		$_output[] = '<span class="itemTotal">' ;
		$_output[] = 'Records per page:&nbsp;' ;
		
		foreach ( $pagerLimits as $thisLimit ) {
			if ( $thisLimit == $pager->limit ) {
				$_output[] = '<span style="font-weight:bold">'.$thisLimit.'</span>' ;
			} else {
				// note that in original tpl code we tried to set the page number intelligently when the pager was changed. Here we're just resetting to page 1.
				$_output[] = '<a href="'.$basePath.'?'.PAGER_KEY_LIMIT.'='.$thisLimit.'&amp;'.PAGER_KEY_PAGE.'=1'.$get.'">'.$thisLimit.'</a>' ;
			}
		}
		
		if ( $enableAll ) {
			if ( $pager->showmaxrecords ) {
				$_output[] = '<span style="font-weight:bold">all</span>' ;
			} else {
				$_output[] = '<a href="'.$basePath.'?'.PAGER_KEY_LIMIT.'=max'.$get.'">all</a>' ;
			}
		
		}

		$_output[] = '&nbsp;&nbsp;('.$pager->totalrecs.' total)' ;
		$_output[] = '</span></div>' ;
		
		return implode( "\n", $_output ) ;


	}

	public function __destruct() {

	}


}
